package com.net.framework.help.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;

import com.net.framework.help.R;


/**
 * @author zhangxiaobin
 * @version V1.0
 * @ClassName: CustomProgressDialog.java
 * @email 154722566@qq.com
 * @Date 2016年2月25日 下午4:09:10
 * @Description: web中加载弹窗
 */
public class CustomProgressDialog extends Dialog {
    private static CustomProgressDialog customProgressDialog = null;
    private static Context oldContext;

    public CustomProgressDialog(Context context) {
        super(context);
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    private static CustomProgressDialog createDialog(Context context) {
        customProgressDialog = new CustomProgressDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.layout_web_progressdialog);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        return customProgressDialog;
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        if (customProgressDialog == null) {
            return;
        }
        ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
    }

    public static void showProgressDialog(Context context) {
        if (null != oldContext) {
            if (context != oldContext) {
                customProgressDialog = CustomProgressDialog.createDialog(context);
                customProgressDialog.setCanceledOnTouchOutside(false);
            }
        }

        if (customProgressDialog == null) {
            customProgressDialog = CustomProgressDialog.createDialog(context);
            customProgressDialog.setCanceledOnTouchOutside(false);
        }


        if (!customProgressDialog.isShowing()) {
            customProgressDialog.show();
        }
        oldContext = context;
    }

    public static void dismissProgressDialog() {

        if (customProgressDialog != null && customProgressDialog.isShowing()) {
            customProgressDialog.dismiss();
            customProgressDialog = null;
        }
    }
}